package com.beta.cat.utils;

import java.math.BigDecimal;
import java.util.*;

/**
 * Map处理工具
 *
 * @author kevin_zhang
 * @date 28/11/2017
 */
public class MapUtils {

    public static boolean isEmpty(Map map) {
        if (null == map || map.isEmpty()) {
            return true;
        } else {
            return false;
        }
    }

    public static int getIntValue(Map map, String key, int def) {
        try {
            if (map.containsKey(key)) {
                return Integer.parseInt(String.valueOf(map.get(key)));
            } else {
                return def;
            }
        } catch (Exception e) {
            return def;
        }
    }

    public static float getFloatValue(Map map, String key, float def) {
        if (map.get(key) != null) {
            return (Float) map.get(key);
        }
        return 0.0f;
    }

    public static long getLongValue(Map map, String key, long def) {
        try {
            if (map.containsKey(key)) {
                return Long.parseLong(String.valueOf(map.get(key)));
            } else {
                return def;
            }
        } catch (Exception e) {
            return def;
        }
    }

    public static double getDoubleValue(Map map, String key, long def) {
        try {
            if (map.containsKey(key)) {
                return Double.parseDouble(String.valueOf(map.get(key)));
            } else {
                return def;
            }
        } catch (Exception e) {
            return def;
        }
    }

    public static boolean getBooleanValue(Map<String, Object> map, String key, boolean def) {
        try {
            if (map.containsKey(key)) {
                return ((Boolean) map.get(key)).booleanValue();
            } else {
                return def;
            }
        } catch (Exception e) {
            return def;
        }
    }

    public static String getStringValue(Map map, String key, String def) {
        try {
            if (map.containsKey(key)) {
                return String.valueOf(map.get(key));
            } else {
                return def;
            }
        } catch (Exception e) {
            return def;
        }
    }

    public static Date getDateValue(Map<String, Object> map, String key, Date def) {
        try {
            if (map.containsKey(key)) {
                return (Date) map.get(key);
            } else {
                return def;
            }
        } catch (Exception e) {
            return def;
        }
    }

    public static List getListValue(Map map, String key, List def) {
        try {
            if (map.containsKey(key)) {
                return (List) map.get(key);
            } else {
                return def;
            }
        } catch (Exception e) {
            return def;
        }
    }

    public static BigDecimal getBigDecimalValue(Map map, String key, BigDecimal def) {
        try {
            if (map.containsKey(key)) {
                return new BigDecimal(String.valueOf(map.get(key)));
            } else {
                return def;
            }
        } catch (Exception e) {
            return def;
        }
    }

    /**
     * Map转请求参数字符串
     *
     * @param params 有序Map
     * @return
     */
    public static String map2LinkStr(LinkedHashMap<String, String> params) {
        String linkStr = "";
        Iterator iterator = params.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry entry = (Map.Entry) iterator.next();
            String key = entry.getKey().toString();
            String value = entry.getValue().toString();
            if (iterator.hasNext()) {
                linkStr += key + "=" + value + "&";
            } else {
                linkStr += key + "=" + value;
            }
        }
        return linkStr;
    }

    /**
     * Map转请求参数字符串
     *
     * @param params 无序Map
     * @return
     */
    public static String map2LinkStr(Map<String, String> params) {
        List<String> keys = new ArrayList<>(params.keySet());
        //排序-升序
        Collections.sort(keys);

        String linkStr = "";
        for (int i = 0; i < keys.size(); i++) {
            String key = keys.get(i);
            String value = params.get(key);

            if (i == keys.size() - 1) {
                linkStr += key + "=" + value;
            } else {
                linkStr += key + "=" + value + "&";
            }
        }
        return linkStr;
    }
}
